package com.huayun.code.core.gen;

import com.huayun.code.core.utils.ConfigUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public abstract class AbstractGenerator implements CodeGenerator, InitializingBean {

    @Autowired
    private ConfigUtils configUtils;

    private Map<String, Object> cfg = new HashMap<>();

    public static final String MODEL = "model";
    public static final String MODEL_QUERY = "modelQuery";
    public static final String SQL_MAPPER = "sqlMapper";
    public static final String JAVA_MAPPER = "javaMapper";
    public static final String SERVICE = "service";
    public static final String CONTROLLER = "controller";

    public AbstractGenerator() {

    }


    @Override
    public void afterPropertiesSet() {
        String basePackage = ConfigUtils.getBasePackage();
        cfg.put("modelPackage", basePackage + ".bean.entity");
        cfg.put("modelQueryPackage", basePackage + ".bean.query");
        cfg.put("sqlMapperPackage", basePackage + ".mapper");
        cfg.put("javaMapperPackage", basePackage + ".repository");
        cfg.put("servicePackage", basePackage + ".repository");
        cfg.put("controllerPackage", basePackage + ".service");

        cfg.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    protected Map<String, Object> getCfg() {
        return cfg;
    }

    protected String getPackage(String type) {
        return (String) cfg.get(type + "Package");
    }

}
